package com.qingzhuge.manager.entity;


import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.qingzhuge.common.enums.ESexType;
import com.qingzhuge.common.validator.AddGroup;
import com.qingzhuge.common.validator.UpdateGroup;
import com.qingzhuge.entity.BaseModel;
import com.qingzhuge.interfaces.ISysUser;
import com.qingzhuge.resource.entity.SysResource;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.builder.ToStringBuilder;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author : zero.xiao
 * @description : 系统用户
 * @date :2018/6/8 0008 下午 15:02
 * @modified :
 * @since : 1.0.0
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "sys_user")
public class SysUser extends BaseModel implements ISysUser {
    private static final long serialVersionUID = 6726754030355343274L;
    private Boolean root;
    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String username;

    /**
     * 密码
     */
    @JSONField(serialize = false)
    private String password;

    /**
     * 角色列表
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private List<SysRole> roles = Lists.newArrayList();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_resource",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id")})
    private Set<SysResource> resources = Sets.newConcurrentHashSet();

    /**
     * 部门ID
     */
    private Long deptId;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 性别(unknown:未知;man:男;woman:女)
     */
    private ESexType sex;
    /**
     * 头像
     */
    private String avatar;
    /**
     * 手机号
     */
    private String mobile;
    /**
     * 身份证号码
     */
    private String idCardNumber;
    /**
     * 微信
     */
    private String weiXin;
    /**
     * 微博
     */
    private String weiBo;
    /**
     * QQ
     */
    private String qq;
    /**
     * 出生日期
     */
    private Date birthDay;
    /**
     * 职位
     */
    private String position;

    /**
     * 详细地址
     */
    private String address;
    /**
     * 工号
     */
    private String staffNo;

    /**
     * 当前登录ip
     */
    private String currentLoginHost;
    /**
     * 上次登录ip
     */
    private String lastLoginHost;
    /**
     * 上次登录时间
     */
    private Date lastLoginTime;
    /**
     * 当前登录时间
     */
    private Date currentLoginTime;
    /**
     * 登录次数
     */
    private Integer loginCount;
    /**
     * 登录错误次数,用于用户登录次数过多时休眠用户登录状态
     */
    @JSONField(serialize = false)
    private Integer loginErrorCount;

    @Transient
    private String permission;

    public SysUser() {
    }

    public SysUser(Long id) {
        super(id);
    }

    public SysUser(Long id,String name){
        super(id);
        super.setName(name);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
